/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution
{
public:
    int numComponents(ListNode *head, vector<int> &nums)
    {
        unordered_set<int> numSet(nums.begin(), nums.end());
        bool continuous = false;
        int componentCount = 0;
        while (head != nullptr)
        {
            if (numSet.count(head->val))
            {
                if (!continuous)
                {
                    ++componentCount;
                    continuous = true;
                }
            }
            else
            {
                continuous = false;
            }
            head = head->next;
        }
        return componentCount;
    }
};